//
//  putMyConsultViewController.m
//  Toyohu
//
//  Created by Heige on 16/5/9.
//  Copyright © 2016年 celia. All rights reserved.
//

#import "putMyConsultViewController.h"
#import <Masonry.h>
#define WIDTH self.view.bounds.size.width
#define HEIGHT self.view.bounds.size.height
@interface putMyConsultViewController ()<UITextViewDelegate>
{
    UITextView *myTextView;
    UIView * tabBarView;
}

@end

@implementation putMyConsultViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.hidden = YES;
    [self CreateNavView];
    [self CreateTextView];
  //  [self CreateToolBar];
    [self CreateVoiceButton];
    
}
-(void)CreateNavView
{
    UIView *navView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH ,64)];
    navView.backgroundColor = [UIColor colorWithRed:253/255.0 green:214/255.0 blue:17/255.0 alpha:1];
    [self.view addSubview:navView];
    
    
    //导航栏左边项
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeSystem];
    leftButton.frame = CGRectMake(20, 35, 10, 20);
    [leftButton setBackgroundImage:[UIImage imageNamed:@"ARROW---LEFT@2x"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(goBackPreviousPage) forControlEvents:UIControlEventTouchDown];
    
    [navView addSubview: leftButton];
    
    UILabel *navLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
    navLabel.center = CGPointMake(navView.frame.size.width/2, navView.frame.size.height/2+10);
    navLabel.textColor = [UIColor whiteColor];
    navLabel.backgroundColor = [UIColor clearColor];
    navLabel.textAlignment = NSTextAlignmentCenter;
    navLabel.text = @"发布倾诉";
    navLabel.font = [UIFont systemFontOfSize:20];
    [navView addSubview:navLabel];
    
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [rightButton addTarget:self action:@selector(sendMessage) forControlEvents:UIControlEventTouchDown];
    [rightButton setTitle:@"发送" forState:UIControlStateNormal];
    [rightButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [navView addSubview:rightButton];
    [rightButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.equalTo(navView.mas_trailing).offset(-20);
        make.width.equalTo(@(35));
        make.height.equalTo(@(40));
        make.centerY.equalTo(navLabel.mas_centerY);
    }];
}
-(void)CreateTextView
{
    myTextView = [[UITextView alloc]initWithFrame:CGRectMake(10, 64, WIDTH-20, 160)];
    myTextView.backgroundColor= [UIColor whiteColor];
    myTextView.text = @"敞开你的心扉";
    myTextView.textColor = [UIColor grayColor];
    myTextView.delegate = self;
    myTextView.font = [UIFont systemFontOfSize:18];
    
    [self.view addSubview:myTextView];
    UIView *spaceView = [[UIView alloc]initWithFrame:CGRectMake(0, 224, WIDTH, 1)];
    spaceView.backgroundColor = [UIColor colorWithRed:244/255.0 green:244/255.0 blue:244/255.0 alpha:1];
    [self.view addSubview:spaceView];
}

-(void)CreateToolBar
{
    tabBarView = [[UIView alloc]initWithFrame:CGRectMake(0, 224, WIDTH, 35)];
    tabBarView.layer.borderWidth = 1;
    tabBarView.layer.borderColor = [UIColor colorWithRed:244/255.0 green:244/255.0 blue:244/255.0 alpha:1].CGColor;
    
    [self.view addSubview:tabBarView];
    
    UIButton *cameraButton = [UIButton buttonWithType:UIButtonTypeSystem];
    cameraButton.frame = CGRectMake(0, 0, 24, 15);
    cameraButton.center = CGPointMake(7*tabBarView.frame.size.width/8, tabBarView.frame.size.height/2);
    [cameraButton setBackgroundImage:[UIImage imageNamed:@"photo_camera_47.579390115668px_1200120_easyicon.net"] forState:UIControlStateNormal];
    [cameraButton addTarget:self action:@selector(doCamera) forControlEvents:UIControlEventTouchDown];
    [tabBarView addSubview:cameraButton];
    
    
    UIButton *pictureButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [pictureButton setBackgroundImage:[UIImage imageNamed:@"Picture_45.241379310345px_1198601_easyicon.net"] forState:UIControlStateNormal];
    [pictureButton addTarget:self action:@selector(getPicture) forControlEvents:UIControlEventTouchDown];
    [tabBarView addSubview:pictureButton];
    [pictureButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(cameraButton.mas_left).offset(-40);
        make.centerY.equalTo(cameraButton.mas_centerY);
        make.width.equalTo(@(24));
        make.height.equalTo(@(15));
    }];

}
/**
 *  注意 使用masonry限制控件在视图中间的时候 不能再限制它距离上一个视图的垂直距离
 */
-(void)CreateVoiceButton
{
    UIButton *voiceButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [voiceButton setBackgroundImage:[UIImage imageNamed:@"yuyin.png"] forState:UIControlStateNormal];
    [self.view addSubview:voiceButton];
    
    [voiceButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.width.height.equalTo(@(95));
        make.center.equalTo(self.view);
    }];
    
    UILabel *label = [[UILabel alloc]init];
    label.text = @"58S";
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont systemFontOfSize:18];
    [self.view addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(voiceButton.mas_centerX);
        make.top.equalTo(voiceButton.mas_bottom).offset(5);
        make.width.equalTo(@(60));
        make.height.equalTo(@(30));
    }];
    
    
}
#pragma mark - UITextViewDelegate
- (void)textViewDidEndEditing:(UITextView *)textView
{
    if(textView.text.length < 1){
        textView.text = @"敞开你的心扉";
        textView.textColor = [UIColor grayColor];
    }
}
- (void)textViewDidBeginEditing:(UITextView *)textView
{
    if([textView.text isEqualToString:@"敞开你的心扉"]){
        textView.text=@"";
        textView.textColor=[UIColor blackColor];
    }
}


- (void)textViewDidChange:(UITextView *)textView {
    //只要前240个字
    if (textView.text.length > 200) {
        textView.text = [textView.text substringWithRange:NSMakeRange(0, 200)];
    }
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    if (textView.text.length == 200 && range.length == 0) {
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"发布内容超过200字了~" message:@"" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alertView show];
        return NO;
    }
    return YES;
}

#pragma mark-- Customed method
-(void)goBackPreviousPage
{
    [self dismissViewControllerAnimated:YES completion:nil];
}
-(void)sendMessage
{
    
}

-(void)doCamera
{
    
}

-(void)getPicture
{
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
